package preprocessing;

import preprocessing.Stemmer;
import utils.FileStorageUtil;
import utils.StopWords;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class EnglishPreprocessor implements Preprocessor {
    private static final Pattern NON_ALPHABETIC = Pattern.compile("[^a-zA-Z\\s]");
    private final StopWords stopWords;

    public EnglishPreprocessor() throws IOException {
        this.stopWords = new StopWords("src/main/resources/stopwords/english_stopwords.txt");
    }

    @Override
    public String preprocessText(String text) throws IOException {
        //移除 HTML 标签
        String noHtml = text.replaceAll("<[^>]+>", "");

        // 删除特殊字符
        String onlyAlphabetic = NON_ALPHABETIC.matcher(noHtml).replaceAll(" ");

        // 小写转换
        String lowercase = onlyAlphabetic.toLowerCase();

        // 停用词
        List<String> tokens = tokenize(lowercase);

        // 移除停用词
        List<String> noStopWords = tokens.stream()
                .filter(token -> !stopWords.isStopWord(token))
                .collect(Collectors.toList());

        // 词干提取
        Stemmer stemmer = new Stemmer();
        List<String> stemmedTokens = noStopWords.stream()
                .map(stemmer::stem)
                .collect(Collectors.toList());

        return String.join(" ", stemmedTokens);
    }

    @Override
    public List<String> tokenize(String text) {
        return Arrays.stream(text.split("\\s+"))
                .filter(token -> !token.isEmpty())
                .collect(Collectors.toList());
    }

    @Override
    public void preprocessFile(File inputFile, File outputFile) throws IOException {
        String content = new String(Files.readAllBytes(inputFile.toPath()));
        String processedContent = preprocessText(content);

        // 确保输出目录存在
        Path outputDir = outputFile.toPath().getParent();
        if (outputDir != null) {
            FileStorageUtil.ensureDirectoryExists(outputDir.toString());
        }

        Files.write(outputFile.toPath(), processedContent.getBytes("UTF-8"));
    }

    public void preprocessAndSave(File inputFile, String outputDir, String prefix) throws IOException {
        String content = new String(Files.readAllBytes(inputFile.toPath()));
        String processedContent = preprocessText(content);

        String filename = FileStorageUtil.generateUniqueFilename(prefix, "txt");
        FileStorageUtil.saveDocument(processedContent, outputDir, filename);
    }
}